package commonFunction;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;

public class ReadExcelData {
	/**
	 * 读取excel数据
	 * 
	 * @throws IOException
	 */

	@DataProvider(name = "loginData")
	public static Object[][] testData() throws IOException {
		return getData();
	}

	@SuppressWarnings("resource")
	public static Object[][] getData() throws IOException {
		InputStream is = new FileInputStream("testData/login.xlsx");// 文件相对路径以项目根目录为起点
		XSSFWorkbook workBook = new XSSFWorkbook(is);

		XSSFSheet sheet = workBook.getSheetAt(0);// 获取第一个sheet页
		int rowNum = sheet.getLastRowNum();// 获取总行数
		List<Object[]> records = new ArrayList<Object[]>();

		for (int i = 1; i <= rowNum; i++) {// i=1，从第2行开始读，第一行为标题
			XSSFRow row = sheet.getRow(i);
			int colNum = row.getLastCellNum();
			String[] data = new String[colNum];

			for (int j = 0; j < colNum; j++) {
				data[j] = row.getCell(j).getStringCellValue();
			}
			records.add(data);
		}
		// 转换成Object[][]
		Object[][] results = new Object[records.size()][];
		for (int i = 0; i < records.size(); i++) {
			results[i] = records.get(i);
			// System.out.println(results[i]);
		}
		return results;
	}

}
